package ar.edu.sccs.util;

import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.ObjectInputStream;
import java.util.ArrayList;
import java.util.List;

import ar.edu.sccs.model.loader.dto.DataClazz;
import ar.edu.sccs.monitoring.exception.SccsGenericException;

public class ObjectInStream {
	private ObjectInputStream file2read;

	public List<DataClazz> readFile(String filePath) throws SccsGenericException {
		List<DataClazz> dataClazz = new ArrayList<DataClazz>();

		try {
			openFile(filePath);
		} catch (Exception e1) {
			throw new SccsGenericException(e1);
		}

		while (true) {
			try {
				dataClazz.add((DataClazz) file2read.readObject());
			} catch (EOFException eofe) {
				break;
			} catch (Exception e2) {
				throw new SccsGenericException(e2);
			}
		}

		closeFile();
		return dataClazz;
	}

	private void openFile(String filePath) throws Exception {
		file2read = new ObjectInputStream(new FileInputStream(
				new File(filePath)));
	}

	private void closeFile() {
		try {
			file2read.close();
		} catch (Exception e) {
		}
	}
}
